$(function() {
    // 渲染
    function renderSwiper() {
        $.ajax({
            url: '/admin/swipers',
            method: 'GET',
            success: function(res) {
                if (res.status != 0) {
                    return layer.msg(res.message)
                }
                var htmlStr = template('swiper-tpl', res)
                $('tbody').html(htmlStr)
            }
        })
    }
    renderSwiper()
        // 修改状态
    $(document).on('click', '.layui-badge', function(e) {
            $(e.target).show().siblings().hide()
            var status = $(e.target).data('status')
            var id = $(e.target).data('id')
            $.ajax({
                url: '/admin/swipers/' + id,
                method: 'PUT',
                data: {
                    status
                },
                success: function(res) {
                    if (res.status != 0) {
                        return layer.msg(res.message)
                    }
                    layer.msg(res.message)
                    renderSwiper()
                }
            })
        })
        // 批量上传
    $('#uploadSwiper').click(function() {
        $('#myfile').click()
    })
    $('#myfile').change(function() {
            var files = this.files
            if (files.length == 0) {
                return layer.msg('请上传封面图片')
            }
            var fd = new FormData();
            for (var i = 0; i < files.length; i++) {
                fd.append('swipers', files[i])
            }
            $.ajax({
                url: '/admin/swipers',
                method: 'POST',
                data: fd,
                processData: false,
                contentType: false,
                success: function(res) {
                    if (res.status != 0) {
                        return layer.msg(res.message)
                    }
                    layer.msg(res.message)
                    renderSwiper()
                }
            })
        })
        // 删除
    $(document).on('click', '.delete', function() {
        var id = $(this).data('id')
        layer.confirm('确定要删除吗?', { icon: 3, title: '提示' }, function(index) {
            $.ajax({
                url: '/admin/swipers/' + id,
                method: 'DELETE',
                data: '',
                success: function(res) {
                    if (res.status != 0) {
                        return layer.msg(res.message)
                    }
                    layer.msg('删除分类成功!')
                    renderSwiper()
                }
            })
            layer.close(index);
        })
    })

})